<?php
/**
 * Created by PhpStorm.
 * User: SYSTEM
 * Date: 2018-8-15
 * Time: 11:24
 */

header('Content-Type: text/html; charset=utf-8');
//页面基本参数初始化
$pageSize = 5; //每一页显示多少行数的数据
$showpage = 5; //分页条显示几个指定按钮


//1、传入页码
$page = isset($_GET['p']) ? $_GET['p'] : 1;

//一页显示多少条记录

//2、根据页码取出数据：php->mysql处理
try{
    //PDO链接数据库
    $pdo = new PDO('mysql:host=localhost; dbname=phptest', 'root', 'root');
    //执行的查询语句
    //编写sql获取分页数据SLECT * FROM 表名 LIMIT 起始位置，显示条数
    $sql = "select * from computermenu LIMIT ".(($page-1)*$pageSize).",".$pageSize;
    //$sql = "select * from computermenu";
    //把sql语句传送到数据库
    $stmt = $pdo->query($sql);

    echo <<<eof
        <style type="text/css">
            a:link, a:visited, a:hover; a:active{
                color: #000000;
            }
            table{
                width: 850px;
                margin: auto;   
                border: 1px solid black;
                border-collapse: collapse;
            }
            td{
                border: 1px solid black;
                text-align: center;
            }
            .pageclass{
                padding: 10px 0px;
                font-size: 13px;
            }
            .pageclass a{
                border: #aaaadd 1px solid;
                text-decoration: none;
                padding: 2px 5px;
                margin: 2px;
            }
            .pageclass a:hover{
                border: #3ccedd 1px solid;
            }
            .pageclass span.current{
                background-color: #0a22ca;
                padding: 1px;
                
                border: #0a22ca 1px solid;
            }
            span.current a{
                border: #0a22ca 0px solid;
                color: #ffffff;
                font-weight: bold;
            }
            div.pageclass .disable{
                border: #979ba3 1px solid;
                margin: 2px;
                padding: 2px 5px;
                color: #979ba3;
            }
            div.pageclass form{
                display: inline;
            }
        </style>
        <table>
            <tr style="background-color: #e6ffec;">
                <td>序号</td>
                <td>电脑名称</td>
                <td>电脑颜色</td>
                <td>价格</td>
            </tr>
eof;


    foreach($stmt as $row){
        echo '<tr>';
        echo '<td>'.$row['id'].'</td>';
        echo '<td>'.$row['computername'].'</td>';
        echo '<td>'.$row['computercolor'].'</td>';
        echo '<td>'.$row['computerprice'].'</td>';
        echo '</tr>';
    }

    //3、显示数据+分页条
    //获取当前php当前文件
    $self = $_SERVER['PHP_SELF'];
    //查询数据库中数据的条数
    $total_sql = 'select * from computermenu';
    $total_stmt = $pdo->query($total_sql);
    $total = $total_stmt->rowCount();





    //计算页数
    $total_page = ceil($total/$pageSize);

    $page_banner = "";



    //计算偏移量
    $pageoffset = ($showpage-1)/2;

    //判断是否显示首页与上一页
    if($page > 1){
        $page_banner .= '<a href="'.$self."?p=".(1).'">首页</a>';
        $page_banner .= '<a href="'.$self."?p=".($page-1).'">上一页</a>';
    }else{
        $page_banner .= '<a class="disable">首页</a>';
        $page_banner .= '<a class="disable">上一页</a>';
    }


    //初始化数据
    $start = 1;
    $end =  $total_page;

    //如果总页数大于显示的条数
    if($total_page > $showpage){
        //如果页面大于偏移量加1
        if($page > $pageoffset + 1){
            $page_banner .= '...';
        }

        if($page > $pageoffset){
            $start = $page - $pageoffset;
            $end = $total_page > $pageoffset ? $page + $pageoffset : $total_page;
        }else{
            $start =1;
            $end = $total_page > $showpage ? $showpage : $total_page;
        }

        if($page+$pageoffset > $total_page){
            $start = $start - ($page + $pageoffset - $end);
        }
    }
    for($i=$start; $i<=$end; $i++){
        if($page == $i){
            $page_banner .= '<span class="current"><a href="'.$self."?p=".$i.'">'.$i.'</a></span>';
        }else{
            $page_banner .= '<a href="'.$self."?p=".$i.'">'.$i.'</a>';
        }
    }

    //尾部省略
    if($total_page > $showpage && $total_page > $page+$pageoffset){
        $page_banner .= "...";
    }

    if($page < $total_page){
        $page_banner .= '<a href="'.$self."?p=".($page+1).'">下一页</a>';
        $page_banner .= '<a href="'.$self."?p=".($total_page).'">尾页</a>';
    }else{
        $page_banner .= '<a class="disable">下一页</a>';
        $page_banner .= '<a class="disable">尾页</a>';
    }


    //显示多少条记录
    $page_banner .= " 总".$total."条 &nbsp;";
    //显示一共有多少页
    $page_banner .= "&nbsp; 共".$total_page."页，第".$page."页";

    //自定义跳转页面
    $page_banner .= '<form action="'.$self.'" method="get">';
    $page_banner .= '到第<input type="text"  name="p" style="height: 22px; width: 50px;">页';
    $page_banner .= '<input type="submit" value="确定" style="height: 22px;">';
    $page_banner .= '</form>';

    echo '<tr><td colspan="4"><div class="pageclass">';
    echo $page_banner;
    echo '</div></td></tr>';
    echo '</table>';



}catch(PDOException $e){
    echo $e->getMessage();
}
